using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.System;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;

public partial class App_Input_NotifyMessageDepartForm : System.Web.UI.Page
{
    private IList chkDepartList = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            hidDepartId.Value = Request.QueryString["departId"];
            ViewState["ControlId"] = Request.QueryString["cntId"];
        }
        
        string[] s = hidDepartId.Value.Split(',');
        this.chkDepartList = new ArrayList(s);
    }

    protected override void OnPreRender(EventArgs e)
    {
        NotifyMessageDao dao = new NotifyMessageDao();
        IList dataList = dao.GetDepartmentList();
        GridViewResult.DataSource = dataList;
        GridViewResult.DataBind();
    }

    protected void GridViewResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewResult.PageIndex = e.NewPageIndex;
    }

    protected void GridViewResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == -1)
            return;

        MstDepartmentBean dataBean = (MstDepartmentBean)e.Row.DataItem;

        HtmlInputCheckBox chkDepartId = new HtmlInputCheckBox();
        chkDepartId.ID = "departId";
        chkDepartId.Value = dataBean.DepartId;
        chkDepartId.Attributes.Add("onchange", "onCheckDepart(this)");
        chkDepartId.Checked = (this.chkDepartList.Contains(dataBean.DepartId));
        e.Row.Cells[2].Controls.Add(chkDepartId);
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        MessageBean paramBean = new MessageBean();
        paramBean.DepartIdList = this.chkDepartList;

        NotifyMessageDao dao = new NotifyMessageDao();
        IList dataList = dao.FindDepartByMessage(paramBean);

        string script = string.Format("var obj = opener.window.document.getElementById('{0}');\r\n", ViewState["ControlId"]);
        script += "if( obj!=null ) { \r\n";
        script += "obj.options.length = 0;\r\n";
        IEnumerator enm = dataList.GetEnumerator();
        while (enm.MoveNext())
        {
            MstDepartmentBean dataBean = (MstDepartmentBean)enm.Current;
            script += "var option = opener.window.document.createElement('OPTION'); \r\n";
            script += string.Format("option.value='{0}';",dataBean.DepartId);
            script += string.Format("option.text='{0}';", dataBean.DepartName);
            script += "obj.add( option );\r\n";
        }
        script += "opener.window.focus();window.close();}\r\n";
        
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", script,true);
    }
}
